﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using UDP_WPF;

/// <summary>
/// 网络数据发送
/// DataSend.Instance().send_PACTET_CONNECT(this.Handle,"Progman");
/// </summary>

class DataSend
{
    private delegate void RecDelegate(string str, int ActionStyle); //委托

    public static DataSend static_DataSend;
    public Thread threa_send;
    public string send_msg;    //发送的消息
    public IPEndPoint send_ipe; //发送的目标

    private DataSend()
    {
    }

    /// <summary>
    /// send - action -style
    /// </summary>
    public void send_PACTET_MSGSTYLE_NET_CONNECT(IPEndPoint send_ipe)  //发送连接成功消息
    {
        string str = NetPacket.PACTET_MSGSTYLE_NET_CONNECT;
        send(str,send_ipe);
    }
    public void send_PACTET_MSGSTYLE_SET_SENSOR(string content, IPEndPoint send_ipe)  //发送连接成功消息
    {
        string str = NetPacket.PACTET_MSGSTYLE_SET_SENSOR;
        str = str + ":" + content;
        send(str, send_ipe);
    }


    /// <summary>
    /// DataSend 的基本功能
    /// </summary>
    //实例化
    public static DataSend Instance()
    {
        if (static_DataSend == null)
        {
            return new DataSend();
        }
        return static_DataSend;
    }

    //发送
    public void send(string send_msg, IPEndPoint send_ipe)
    {
        this.send_msg = send_msg;
        this.send_ipe = send_ipe;
        if ((this.send_msg == "") || (this.send_ipe == null))
        {
            return;
        }
        if (null != threa_send)
        {
            threa_send.Abort();
            threa_send = null;
        }
        threa_send = new Thread(send_thread);
        threa_send.IsBackground = true;
        threa_send.Start();
    }
    //接受数据线程
    public void send_thread()
    {
        UdpClient returnmsg = new UdpClient();
        try
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(this.send_msg);
            returnmsg.Send(bytes, bytes.Length, this.send_ipe);
            returnmsg.Close();
            fresh(this.send_ipe.Address.ToString(), 1);
            fresh("发送 - 目标：" + this.send_ipe.Address.ToString() + "  内容：" + this.send_msg, 2);
        }
        catch (Exception e)
        {
        }
    }

    //关闭接收线程
    public void stop()
    {
        threa_send.Abort();
    }
    //界面交互委托刷新方法
    public void fresh(string str, int ActionStyle)
    {
        //状态刷新
        switch (ActionStyle)
        {
            case 1:
                if (Driver.static_driver.clientippaddr.InvokeRequired)//等待异步
                {
                    RecDelegate fc = new RecDelegate(fresh);
                    Driver.static_driver.Invoke(fc, str, ActionStyle); //通过代理调用刷新方法
                }
                else
                {
                    Driver.static_driver.clientippaddr.Text = str;
                }
                break;
            case 2:
                if (Driver.static_driver.tb_printftest.InvokeRequired)//等待异步
                {
                    RecDelegate fc = new RecDelegate(fresh);
                    Driver.static_driver.Invoke(fc, str, ActionStyle); //通过代理调用刷新方法
                }
                else
                {
                    Driver.static_driver.tb_printftest.Text = Driver.static_driver.tb_printftest.Text + "\r\n" + str;
                }
                break;
        }
    }
}
